<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Usedcar;
use App\Picture;

class UsedcarController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //$usedcar = Usedcar::get();
        return view('usedcar.index', compact('usedcar'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $req ,$id)
    {
        if(empty($_POST)) {
            return view('usedcar.create', compact('id'));
        } else {
            $file = $req->file('pic');  //获取图片对象
            $date = date('Ymd');
            $destPath = realpath('images').$date;  //根据年月日八位数字为文件夹名字保存图片
            
            if(!$is_dir($destPath)) {
                mkdir($destPath, 0777, true);     //如该文件夹不存在则创建一个
            }
            $fileName= str_random(8).$file->getClientOriginalName();  //给上传文件重新命名
            $file->move($destPath, $fileName);    //把上传图片移动到指定目录

            $picture = new Picture();          //获取图片表的对象
            $picture->name = '标题图片';
            $destPath = '/images/'.$date.'/';
            $picture->src = $destPath.$fileName;  //图片路径
            $picture->url = '#';                  //图片超链接地址
            $picture->type = '#';
            $picture->uses = $id;                //图片使用区域  
            $picture->status = '0';              //图片状态
            $picture->save();

            $usedcar = new Usedcar();
            $usedcar->carseries    = $_POST['carseries'];     //车系
            $usedcar->carmodel     = $_POST['carmodel'];      //车型
            $usedcar->carconf      = $_POST['carconf'];       //配置
            $usedcar->year         = $_POST['year'];          //年份
            $usedcar->price        = $_POST['price'];         //价格
            $usedcar->color        = $_POST['color'];         //颜色
            $usedcar->operation    = $_POST['operation'];     //是否运营
            $usedcar->registertime = $_POST['registertime'];  //上牌时间
            $usedcar->mileage      = $_POST['mileage'];       //行驶里程
            $usedcar->checktime    = $_POST['checktime'];     //年检到期
            $usedcar->insurance    = $_POST['insurance'];     //强险到期
            $usedcar->discharge    = $_POST['discharge'];     //排放标准
            $usedcar->gearbox      = $_POST['gearbox'];       //变速箱
            $usedcar->gestype      = $_POST['gestype'];       //燃油类型
            $usedcar->approve      = $_POST['approve'];       //是否奥迪认证
            $usedcar->guarantee    = $_POST['guarantee'];     //是否有质保
            $usedcar->finance      = $_POST['finance'];       //是否有金融政策
            $usedcar->summary      = $_POST['summary'];       //汽车描述
            $usedcar->pid          = $picture->id;            //获得保存图片主键id
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $req ,$id)
    {
        
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        if(empty($_POST)) {
            $usedcar = Usedcar::find($id);
            $pid     = $usedcar['pid'];
            $picture = Picture::find($pid);
            return view('usedcar.update', compact('usedcar','picture'));
        } else {
            $file = $req->file('pic');  //获取图片对象
            $date = date('Ymd');
            $destPath = realpath('images').$date;  //根据年月日八位数字为文件夹名字保存图片
            
            if(!$is_dir($destPath)) {
                mkdir($destPath, 0777, true);     //如该文件夹不存在则创建一个
            }
            $fileName= str_random(8).$file->getClientOriginalName();  //给上传文件重新命名
            $file->move($destPath, $fileName);    //把上传图片移动到指定目录

            $picture = new Picture();          //获取图片表的对象
            $picture->name = '标题图片';
            $destPath = '/images/'.$date.'/';
            $picture->src = $destPath.$fileName;  //图片路径
            $picture->url = '#';                  //图片超链接地址
            $picture->type = '#';
            $picture->uses = $id;                //图片使用区域  
            $picture->status = '0';              //图片状态
            $picture->save();

            $usedcar = new Usedcar();
            $usedcar->carseries    = $_POST['carseries'];     //车系
            $usedcar->carmodel     = $_POST['carmodel'];      //车型
            $usedcar->carconf      = $_POST['carconf'];       //配置
            $usedcar->year         = $_POST['year'];          //年份
            $usedcar->price        = $_POST['price'];         //价格
            $usedcar->color        = $_POST['color'];         //颜色
            $usedcar->operation    = $_POST['operation'];     //是否运营
            $usedcar->registertime = $_POST['registertime'];  //上牌时间
            $usedcar->mileage      = $_POST['mileage'];       //行驶里程
            $usedcar->checktime    = $_POST['checktime'];     //年检到期
            $usedcar->insurance    = $_POST['insurance'];     //强险到期
            $usedcar->discharge    = $_POST['discharge'];     //排放标准
            $usedcar->gearbox      = $_POST['gearbox'];       //变速箱
            $usedcar->gestype      = $_POST['gestype'];       //燃油类型
            $usedcar->approve      = $_POST['approve'];       //是否奥迪认证
            $usedcar->guarantee    = $_POST['guarantee'];     //是否有质保
            $usedcar->finance      = $_POST['finance'];       //是否有金融政策
            $usedcar->summary      = $_POST['summary'];       //汽车描述
            $usedcar->pid          = $picture->id;            //获得保存图片主键id
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $usedcar = Usedcar::find($id);
        $usedcar->delete();
    }
}
